package com.shh.aiojbackendserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shh.aiojbackendserver.common.ErrorCode;
import com.shh.aiojbackendserver.constant.CommonConstant;
import com.shh.aiojbackendserver.exception.BusinessException;
import com.shh.aiojbackendserver.exception.ThrowUtils;
import com.shh.aiojbackendserver.mapper.QuestionAiMapper;
import com.shh.aiojbackendserver.model.dto.QuestionAi.QuestionAiQueryRequest;
import com.shh.aiojbackendserver.model.entity.Question;
import com.shh.aiojbackendserver.model.entity.QuestionAi;
import com.shh.aiojbackendserver.model.entity.User;
import com.shh.aiojbackendserver.model.vo.QuestionAiVO;
import com.shh.aiojbackendserver.model.vo.QuestionVO;
import com.shh.aiojbackendserver.service.QuestionAiService;
import com.shh.aiojbackendserver.utils.SqlUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author yuppie
* @description 针对表【question_ai(题目)】的数据库操作Service实现
* @createDate 2025-05-04 15:47:05
*/
@Service
public class QuestionAiServiceImpl extends ServiceImpl<QuestionAiMapper, QuestionAi>
    implements QuestionAiService {

    /**
     * 校验题目是否合法
     * @param questionAi
     * @param add
     */
    @Override
    public void validQuestion(QuestionAi questionAi, boolean add) {
        if (questionAi == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String title = questionAi.getTitle();
        String content = questionAi.getContent();
        String tags = questionAi.getTags();
        String answer = questionAi.getAnswer();
        String judgeCase = questionAi.getJudgeCase();
        String judgeConfig = questionAi.getJudgeConfig();
        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(title, content, tags), ErrorCode.PARAMS_ERROR);
        }
        // 有参数则校验
        if (StringUtils.isNotBlank(title) && title.length() > 80) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标题过长");
        }
        if (StringUtils.isNotBlank(content) && content.length() > 8192) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "内容过长");
        }
        if (StringUtils.isNotBlank(answer) && answer.length() > 8192) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "答案过长");
        }
        if (StringUtils.isNotBlank(judgeCase) && judgeCase.length() > 8192) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "判题用例过长");
        }
        if (StringUtils.isNotBlank(judgeConfig) && judgeConfig.length() > 8192) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "判题配置过长");
        }
    }

    @Override
    public QueryWrapper<QuestionAi> getQueryWrapper(QuestionAiQueryRequest questionAiQueryRequest) {
       QueryWrapper<QuestionAi> queryWrapper = new QueryWrapper<>();
        String title =  questionAiQueryRequest.getTitle();
        Long id = questionAiQueryRequest.getId();
        Long userId = questionAiQueryRequest.getUserId();
        String sortField = questionAiQueryRequest.getSortField();
        String sortOrder = questionAiQueryRequest.getSortOrder();

        queryWrapper.like(StringUtils.isNotBlank(title),"title",title);

        queryWrapper.eq(ObjectUtils.isNotEmpty(id),"id",id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId),"userId",userId);

        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;

    }


    @Override
    public Page<QuestionAiVO> getQuestionAiVOPage(Page<QuestionAi> questionAiPage) {
        List<QuestionAi> questionAiList = questionAiPage.getRecords();
        Page<QuestionAiVO> questionAiVOPage = new Page<>(questionAiPage.getCurrent(), questionAiPage.getSize(), questionAiPage.getTotal());
        if (CollectionUtils.isEmpty(questionAiList)) {
            return questionAiVOPage;
        }
        // 填充信息
        List<QuestionAiVO> questionAiVOList = questionAiList.stream().map(questionAi -> {
            QuestionAiVO questionAiVO = QuestionAiVO.objToVo(questionAi);
            return questionAiVO;
        }).collect(Collectors.toList());
        questionAiVOPage.setRecords(questionAiVOList);
        return questionAiVOPage;
    }

    @Override
    public QuestionAiVO getQuestionAiVO(QuestionAi questionAi, HttpServletRequest request) {
        QuestionAiVO questionAiVO = QuestionAiVO.objToVo(questionAi);
        return questionAiVO;
    }

}




